<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/13
 * Time: 18:22
 */
namespace App\Services;
use App\Models\Music;
use Illuminate\Support\Facades\DB;
use App\Models\MusicBackground;
use Illuminate\Support\Facades\Redis;
use App\Models\MusicType;

class MusicBackGroundService
{
    private static $music_info = [];
    private static $_msg = ['code' => 0, 'msg' => ''];

    /**
     * @desc 增加背景音乐
     * @param $params
     * @return array
     */
    public static function add_background_music($params)
    {
        if(!self::_check_data($params)) {
            return self::$_msg;
        }

        $music_data = self::_handle_data($params);
        $music_data['created_at'] = $music_data['updated_at'] = time2date();

        // 入库
        $insert_rs = MusicBackground::insert($music_data);
        if(!$insert_rs) {
            self::_set_msg(10000, '增加模板音乐失败');
            return self::$_msg;
        }

        return self::$_msg;
    }

    /**
     * @desc 修改背景音乐
     * @param $params
     * @return array
     */
    public static function modify_background_music($params)
    {
        if(!self::_check_data($params)) {
            return self::$_msg;
        }

        $music_data = self::_handle_data($params);
        $music_data['updated_at'] = time2date();

        // 修改数据
        $where_arr = ['id' => $params['id']];
        $update_rs = MusicBackground::where($where_arr)->update($music_data);
        if(!$update_rs) {
            self::_set_msg(10000, '修改模板音乐失败');
            return self::$_msg;
        }

        return self::$_msg;
    }

    /**
     * @desc 检测背景音乐数据
     * @param $params
     * @return bool
     */
    private static function _check_data($params)
    {
        if(mb_strlen($params['template_name']) > 50) {
            self::_set_msg(10000, '模板名称不能超过50个字符');
            return false;
        }

        // 检测音乐、音乐类别是否匹配
        if(!self::_check_music_data($params)) {
            return false;
        }

        // 检测模板的有效性
        if(isset($params['id'])) {
            $is_record = MusicBackground::where(['id' => $params['id'], 'is_delete' => 0])->first();
            if(!$is_record) {
                self::_set_msg(10000, '模板音乐数据异常');
                return false;
            }
        }
        // 检测模板名称的长度
        // 检测音乐的有效性及音乐与分类的关系
        // 检测音乐分类的有效性
        // 修改时【检测模板的有效性】

        return true;
    }

    /**
     * @desc 检测音乐、音乐类型是否匹配
     * @param $params
     * @return bool
     */
    private static function _check_music_data($params)
    {
        $music_fields = ['id', 'music_name', 'music_link', 'category', 'time_length'];
        $music_where = ['id' => $params['music_id'],'status' => 0, 'is_delete' => 0];
        $music_info = Music::where($music_where)->first($music_fields);
        if(empty($music_info)) {
            self::_set_msg(10000, '音乐未启用或删除');
            return false;
        }

        self::$music_info = $music_info->toArray();

        if($params['music_cid'] != self::$music_info['category']) {
            self::_set_msg(10000, '音乐与音乐类别不匹配');
            return false;
        }

        $type_info = MusicType::where(['status' => 0, 'enable' => 1, 'id' => $params['music_cid']])->first(['name']);
        if(empty($type_info)) {
            self::_set_msg(10000, '音乐类别未启用或删除');
            return false;
        }

        return true;
    }

    /**
     * @desc 整合数据
     * @param $params
     * @return array
     */
    private static function _handle_data($params)
    {
        $data = [
            'operator_id' => $params['uid'],
            'template_name' => $params['template_name'],
            'music_id' => self::$music_info['id'],
            'music_name' => self::$music_info['music_name'],
            'music_link' => self::$music_info['music_link'],
            'music_cid' => self::$music_info['category'],
            'time_length' => self::$music_info['time_length'],
        ];
        return $data;
    }

    /**
     * @desc 修改模板音乐的状态
     * @param $params
     * @return array
     */
    public static function modify_background_music_status($params) {
        if(!in_array($params['status'], [0, 1])) {
            self::_set_msg(10000, '模板音乐状态值异常');
            return self::$_msg;
        }

        $is_record = MusicBackground::where(['id' => $params['id'], 'is_delete' => 0])->first();
        if(!$is_record) {
            self::_set_msg(10000, '模板音乐数据异常');
            return self::$_msg;
        }

        // 修改数据
        $where_arr = ['id' => $params['id']];
        $data = ['status' => $params['status'], 'operator_id' => $params['uid'], 'updated_at' => time2date()];
        $update_rs = MusicBackground::where($where_arr)->update($data);
        if(!$update_rs) {
            self::_set_msg(10000, '修改模板音乐状态失败');
            return self::$_msg;
        }

        return self::$_msg;
    }

    /**
     * @desc 当音乐停用/删除时，修改替换模板的音乐
     * @param $params
     * @return bool
     */
    public static function replace_background_music($params) {
        $music_list = MusicBackground::where(['music_id' => $params['music_id'], 'is_delete' => 0])->get(['id', 'music_cid'])->toArray();
        if(!$music_list) {
            return true;
        }

        $music_fields = ['id', 'music_name', 'music_link', 'category', 'time_length'];
        foreach($music_list as $music) {

            // 获取该分类下的音乐替换
            $music_where = ['category' => $music['music_cid'],'status' => 0, 'is_delete' => 0];
            $music_info = Music::where($music_where)->first($music_fields);
            if($music_info) {
                $data = [
                    'music_name' => $music_info->music_name,
                    'music_link' => $music_info->music_link,
                    'music_id' => $music_info->id,
                    'time_length' => $music_info->time_length
                ];
            } else {
                $data = [
                    'music_name' => '',
                    'music_link' => '',
                    'music_id' => 0,
                    'time_length' => ''
                ];
            }

            MusicBackground::where(['id' => $music['id']])->update($data);
        }

        return true;
    }

    /**
     * @desc 设置提示信息
     * @param $code
     * @param $msg
     * @return array
     */
    private static function _set_msg($code, $msg)
    {
        return self::$_msg = ['code' => $code, 'msg' => $msg];
    }
}